package models
{	
	import events.DeleteSlideEvent;	
	import flash.events.EventDispatcher;	
	import mx.collections.ArrayCollection;
	import mx.controls.Image;
	
	public class Model extends EventDispatcher
	{
		[Bindable]
		[ArrayElementType("Image")] 
		public var slides:ArrayCollection;
		[Bindable]
		[ArrayElementType("Image")] 
		public var flickrImages:ArrayCollection;
		[Bindable]
		[ArrayElementType("Image")] 
		public var previewSlides:ArrayCollection;
		
		private const previewSize:int = 5;
		private var useMockup:Boolean=false;
		private var currentSlideIndex:int = 0;		
		
		[Bindable]
		public var currentSlide:Image;
		
		public function get slidesLeft():int
		{
			return slides.length-1 - currentSlideIndex;
		}
		
		public function Model():void
		{
			slides = new ArrayCollection();
			previewSlides = new ArrayCollection();
			flickrImages = new ArrayCollection();
			currentSlide = new Image();
			if (useMockup)				
				AdditinalInitialization();
		}	
		public function resetSlideShow ():void
		{
			currentSlideIndex = 0;
			currentSlide = Image(slides.getItemAt(currentSlideIndex));
		}
		
		public function loadFlickrImages(FlickrImagesArray:ArrayCollection):void
		{						
			flickrImages = FlickrImagesArray;	
			
		}
		
		public function addImage(image:Image):void
		{			
			var tmp:Image = new Image();
			tmp.load(image.source);

			
			if (currentSlideIndex >= slides.length-1)
			{
				slides.addItem(tmp);
				currentSlideIndex = slides.length-1; 
			}
			else
			{
				currentSlideIndex++;
				slides.addItemAt(tmp,currentSlideIndex);
			}			
			Image(slides.getItemAt(currentSlideIndex)).id = "selected";
			
			if (currentSlideIndex >0)
				Image(slides.getItemAt(currentSlideIndex-1)).id = null;
			updatePreviewSlides();			
		}
	
		private function updatePreviewSlides():void
		{
			var lowerBorder:int = Math.max(0,currentSlideIndex - Math.floor(previewSize/2));
			var upperBorder:int = Math.min(slides.length,currentSlideIndex + Math.ceil(previewSize/2));
			
			previewSlides = new ArrayCollection(slides.source.slice(lowerBorder,upperBorder));
			
			currentSlide = Image(slides.getItemAt(currentSlideIndex));
		}
		
		public function deleteCurrentSlide():void
		{
			slides.removeItemAt(currentSlideIndex);			
			currentSlideIndex = Math.min(slides.length-1, currentSlideIndex);
			updatePreviewSlides();
		}
		
		public function moveToPreviousSlide():void
		{Image(slides.getItemAt(currentSlideIndex)).id = "";
			currentSlideIndex--;
			currentSlideIndex = Math.max(0, currentSlideIndex);
			Image(slides.getItemAt(currentSlideIndex)).id = "selected";
			updatePreviewSlides();
		}
		
		public function moveToNextSlide():void
		{
			Image(slides.getItemAt(currentSlideIndex)).id = "";
			currentSlideIndex++;
			currentSlideIndex = Math.min(slides.length-1, currentSlideIndex);
			Image(slides.getItemAt(currentSlideIndex)).id = "selected";
			updatePreviewSlides();
		}	
		private function AdditinalInitialization():void
		{
			
			slides = new ArrayCollection();
			var tmp:Image;
			var i:int;
			for (i=1;i<=5;i++)
			{
				tmp = new Image();
				tmp.load("../resourses/image"+i+".jpg");
				slides.addItem(tmp);							
			}			
			flickrImages = new ArrayCollection();		
			for (i=1;i<=5;i++)
			{
				tmp = new Image();
				tmp.load("../resourses/image"+i+".jpg");
				flickrImages.addItem(tmp);
			}	
			currentSlideIndex = 3;
			Image(slides.getItemAt(currentSlideIndex)).id = "selected";		
			updatePreviewSlides();
		}			
	}
}